package com.jk.smartelderlycare.controller;

import com.jk.smartelderlycare.dto.SysSlider;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.security.LoginUser;
import com.jk.smartelderlycare.service.SysSliderServiceFeign;
import com.jk.smartelderlycare.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysSliderController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/19 18:33
 */
@Api(tags = "轮播图管理模块")
@Slf4j
@RestController
@RequestMapping(value = "sysSlider")
public class SysSliderController {

    @Autowired
    private SysSliderServiceFeign sysSliderServiceFeign;
    /**
     * @description: 轮播图后端分页查询
     * @param: [sysSlider, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysSlider>>
     * @author hhk
     * @date: 2024/11/19 18:42
     */
    @ApiOperation(value = "轮播图后端分页查询",httpMethod = "POST",notes = "轮播图后端分页查询")
    @PostMapping(value = "findSysSliderPaginationList")
    public ResponseResult<PagedResponse<SysSlider>> findSysSliderPaginationList(@RequestBody SysSlider sysSlider , @RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysSliderServiceFeign.findSysSliderPaginationList(sysSlider,pageNum,pageSize));
    }
    /**
     * @description: 添加轮播图
     * @param: [sysSlider]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/19 18:48
     */
    @ApiOperation(value = "添加轮播图",httpMethod = "POST",notes = "添加轮播图")
    @PostMapping(value = "addSlider")
    public ResponseResult addSlider(@RequestBody SysSlider sysSlider){
        try {
            sysSliderServiceFeign.addSlider(sysSlider);
            return new ResponseResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"添加失败");
        }
    }
    /**
     * @description: 根据id删除一条数据
     * @param: [sliderId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/19 18:48
     */
    @ApiOperation(value = "根据id删除一条数据",httpMethod = "DELETE",notes = "根据id删除一条数据")
    @DeleteMapping(value = "deleteSysSliderById")
    public ResponseResult deleteSysSliderById(@RequestParam(value = "sliderId") Long sliderId){
        try {
            sysSliderServiceFeign.deleteSysSliderById(sliderId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /**
     * @description: 根据id回显轮播图
     * @param: [sliderId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/19 18:49
     */
    @ApiOperation(value = "根据id回显轮播图",httpMethod = "GET",notes = "根据id回显轮播图")
    @GetMapping(value = "findSysSliderById")
    public ResponseResult findSysSliderById(@RequestParam(value = "sliderId") Long sliderId){
        return new ResponseResult(200,"查询成功",sysSliderServiceFeign.findSysSliderById(sliderId));
    }
    /**
     * @description: 修改轮播图
     * @param: [sysSlider]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/19 18:50
     */
    @ApiOperation(value = "修改轮播图",httpMethod = "PUT",notes = "修改轮播图")
    @PutMapping(value = "updateSysSlider")
    public ResponseResult updateSysSlider(@RequestBody SysSlider sysSlider){
        try {
            LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            sysSlider.setModifyUser(loginUser.getUser().getUserName());
            sysSlider.setModifyDate(new Date());
            sysSliderServiceFeign.updateSysSlider(sysSlider);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 修改轮播图状态
     * @param: [sysSlider]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/19 18:51
     */
    @ApiOperation(value = "修改轮播图状态",httpMethod = "PUT",notes = "修改轮播图状态")
    @PutMapping(value = "updataStutas")
    public ResponseResult updataStutas(@RequestBody SysSlider sysSlider){
        try {
            sysSliderServiceFeign.updataStutas(sysSlider);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }

    /**
     * @description: 上传图片
     * @param: [file, request]
     * @return: java.lang.String
     * @author hhk
     * @date: 2024/11/7 10:54
     */
    @ApiOperation(value = "上传图片",httpMethod = "POST",notes = "上传图片")
    @PostMapping("uploadImg")
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        return FileUtil.uploadFile(file,request);
    }
    /** 
     * @description: 批量删除轮播图
     * @param: [sliderId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/19 19:24
     */ 
    @ApiOperation(value = "批量删除轮播图",httpMethod = "DELETE",notes = "批量删除轮播图")
    @DeleteMapping(value = "deleteSysSliderByIds")
    public ResponseResult deleteSysSliderByIds(@RequestParam List<Long> ids){
        try {
            sysSliderServiceFeign.deleteSysSliderByIds(ids);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
           return new ResponseResult(500,"删除失败");
        }
    }





}
